package com.aoxon.cargo.component;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.aoxon.cargo.R;
import com.aoxon.cargo.activity.PubPicturePagerActivity;
import com.aoxon.cargo.bean.Picture;
import com.aoxon.cargo.cache.SupCache;
import com.aoxon.cargo.loader.FileLoader;
import com.aoxon.cargo.state.DataState;
import com.aoxon.cargo.state.MyStateUtil;
import com.aoxon.cargo.util.DataUtil;
import com.aoxon.cargo.util.ImageUtil;

/**
 * 显示方形图片的组件
 * 
 * @author ZZJ
 * 
 */
public class MySquarePicture {

	private Activity activity;
	private LayoutInflater layoutInflater;
	private LinearLayout llPictureHolder;
	private boolean isRoot = false;
	private boolean isShowDeleteBtn = true;
	private FileLoader imageLoader = new FileLoader(200, 200,false);
	private String tag = "MySquarePicture";
	
	public MySquarePicture(Activity activity, LinearLayout llPictureHolder,
			boolean isRoot, boolean showDeleteBtn) {
		this(activity, llPictureHolder, isRoot);
		this.isShowDeleteBtn = showDeleteBtn;
	}
	public MySquarePicture(Activity activity, LinearLayout llPictureHolder,
			boolean isRoot) {
		this.activity = activity;
		this.layoutInflater = LayoutInflater.from(activity.getBaseContext());
		this.llPictureHolder = llPictureHolder;
		this.isRoot = isRoot;
	}

	public void show(Picture picture) {
		if (picture.getPictureState() != DataState.HAVE_BUT_DELETE
				&& picture.getPictureState() != DataState.DELETE) {
			if (picture.getPictureState() == DataState.INITIAL||
					picture.getPictureState() == DataState.CAMERE_INITIAL) {
				getImageView(picture.getPictureUrl(), false);
			} else {
				getImageView(picture.getPictureUrl(), true);
			}
		}
	}

	/**
	 * 根据路径获取显示图片
	 * 
	 * @param path
	 */
	public void getImageView(String path, final boolean loadFromNet) {
		final View pictureItem = layoutInflater.inflate(
				R.layout.pub_square_picture_item, null);
		final ImageView imageView = (ImageView) pictureItem
				.findViewById(R.id.ivPublicSquarePictureItem);
		final ProgressBar progressBar = (ProgressBar) pictureItem
				.findViewById(R.id.pbPublicSquarePictureItem);
		// 删除
		ImageView ivDelete = (ImageView)pictureItem.findViewById(R.id.ivCameraItemDeletebutton);
		if (isShowDeleteBtn) {
			final String deltePath = path;
			ivDelete.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					llPictureHolder.removeView(pictureItem);
					List<Picture> pictureList = SupCache.pictureList;
					for (int i = 0; i < pictureList.size(); i++) {
						if (pictureList.get(i).getPictureUrl().equals(deltePath)) {
							switch (pictureList.get(i).getPictureState()) {
							case DataState.INITIAL:
								pictureList.remove(i);
								break;
							case DataState.SUCCESS:
								pictureList.get(i)
										.setPictureState(DataState.DELETE);
								break;
							case DataState.ADD:
								pictureList.get(i).setPictureState(
										DataState.HAVE_BUT_DELETE);
							default:
								break;
							}
							break;
						}
					}
					Log.v(tag,DataUtil.gson.toJson(SupCache.pictureList));
				}
			});
		} else {
			ivDelete.setVisibility(View.GONE);
		}
		
		try {
			pictureItem.setTag(path);
			pictureItem.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					SupCache.fromCamera = !loadFromNet;
					Intent intent = new Intent(activity.getBaseContext(),
							PubPicturePagerActivity.class);
					PubPicturePagerActivity.ROOT = isRoot;
					activity.startActivityForResult(intent,
							MyStateUtil.GALLERY_RETURN);
				}
			});

			if (loadFromNet) {
				path = path + "s.png";
				imageLoader.displayImage(path, imageView, progressBar, false);
			} else {
				Bitmap bitmap = ImageUtil.getImageBitmap(path, 200, 200);
				imageView.setImageBitmap(ImageUtil.imageCropToSquare(bitmap));
				progressBar.setVisibility(View.GONE);

			}
			llPictureHolder.addView(pictureItem);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
